package com.wsk.tool;

import com.alibaba.fastjson.JSON;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Deprecated
/**
 * 已经舍弃
 */
public class Pornographic {
	private final static String url="https://openapi.baidu.com/rest/2.0/vis-antiporn/v1/antiporn";
	private final static String access_token="23.56408ed1f7566a241a5639875f615a2c.2592000.1486968450.1094025852-9152927";

	public static String CheckPornograp(String image){
		String result="正常图片";
		try {
			String base64 = GetImageStr(image);
			Map<String, String> params = new HashMap<>();
			params.put("access_token", access_token);
			params.put("image", base64);
			String sr = HttpUtils.submitPostData(url, params, "utf-8", "POST");
			RecognitionResultBean recognitionResultBean = JSON.parseObject(sr, RecognitionResultBean.class);
			ArrayList<RecognitionResultBean.ResultArrayClass> list = recognitionResultBean.getResult();
			double a = 0, b = 0, c;
			for (RecognitionResultBean.ResultArrayClass rr : list) {
				if (rr.getClass_name().equals("一般色情"))
					a = rr.getProbability();
				else if (rr.getClass_name().equals("卡通色情"))
					b = rr.getProbability();
				c = Math.max(a, b);
				if (c > 0.25) {
					result = "色情图片";
					break;
				}
			}
			System.out.println(result);
		} catch (NullPointerException e) {
//			e.printStackTrace();
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}
	private static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		byte[] data = null;

// 读取图片字节数组
		try {
			InputStream in = new FileInputStream(imgFilePath);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

// 对字节数组Base64编码
//        Base64 encoder = new Base64();
//        return encoder.encodeAsString(data);
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);// 返回Base64编码过的字节数组字符串
	}
}
